package com.skt.test.pastest;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import com.skt.test.controller.AlarmManagerController;
import com.skt.test.service.UpdateWidgetService;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

public class MySimpleWidget extends AppWidgetProvider {
	private static final String TAG = "PAS";
	
	public static final String CUSTOM_INTENT_TEST_0 = "CUSTOM_INTENT_TEST_0";
	public static final String CUSTOM_INTENT_TEST_1 = "CUSTOM_INTENT_TEST_1";
	public static final String CUSTOM_INTENT_TEST_2 = "CUSTOM_INTENT_TEST_2";
	public static final String CUSTOM_INTENT_TEST_3 = "CUSTOM_INTENT_TEST_3";
	public static final String CUSTOM_INTENT_TEST_4 = "CUSTOM_INTENT_TEST_4";
	
	
	@Override
	public void onReceive(Context context, Intent intent) {
		// Protect against rogue update broadcasts (not really a security issue,
        // just filter bad broacasts out so subclasses are less likely to crash).
        String action = intent.getAction();
        
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
		      Log.d("PasTest", "Intent received");
		      
		      AlarmManagerController amController = new AlarmManagerController(context);
		      int i = amController.restoreAlarm();
		      Log.d(TAG, "BOOT_COMPLETED : " + String.valueOf(i));
		      ComponentName cn = new ComponentName(context.getPackageName(), UpdateWidgetService.class.getName());
		      ComponentName svcName = context.startService(new Intent().setComponent(cn));
		      if (svcName == null) 
		        Log.e("PasTest", "Could not start service " + cn.toString());
		}
        
        if ("CUSTOM_INTENT_TEST_0".equals(action)){
        	Log.d("MyTestWidget", "CUSTOM_INTENT_TEST_0 CALL!!!!!!");
        	Toast.makeText(context, "CUSTOM_INTENT_TEST_0 CALL!!!!!!", Toast.LENGTH_LONG).show();
//        	Bundle bun = intent.getExtras();
//        	int uniqueId = bun.getInt("TESTKEY");
        	
        	//Intent testIntent = new Intent(context, MySimpleWidget.class);
        	
        	//AlarmManagerController amController = new AlarmManagerController(context);
        	//amController.cancelAlarm(testIntent, uniqueId);
        }
        
        if ("CUSTOM_INTENT_TEST_1".equals(action)){
        	Log.d("MyTestWidget", "CUSTOM_INTENT_TEST_1 CALL!!!!!!");
        	Toast.makeText(context, "CUSTOM_INTENT_TEST_1 CALL!!!!!!", Toast.LENGTH_SHORT);
        }
        
        if ("CUSTOM_INTENT_TEST_2".equals(action)){
        	Log.d("MyTestWidget", "CUSTOM_INTENT_TEST_2 CALL!!!!!!");
        	Toast.makeText(context, "CUSTOM_INTENT_TEST_2 CALL!!!!!!", Toast.LENGTH_SHORT);
        }
        
        if ("CUSTOM_INTENT_TEST_3".equals(action)){
        	Log.d("MyTestWidget", "CUSTOM_INTENT_TEST_3 CALL!!!!!!");
        	Toast.makeText(context, "CUSTOM_INTENT_TEST_3 CALL!!!!!!", Toast.LENGTH_SHORT);
        }
        
        if ("CUSTOM_INTENT_TEST_4".equals(action)){
        	Log.d("MyTestWidget", "CUSTOM_INTENT_TEST_4 CALL!!!!!!");
        	Toast.makeText(context, "CUSTOM_INTENT_TEST_4 CALL!!!!!!", Toast.LENGTH_SHORT);
        }
        
        if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {
        	Log.d("MyTestWidget", "ACTION_APPWIDGET_UPDATE CALL!!!!!!");
        	
            Bundle extras = intent.getExtras();
            
            if (extras != null) {
                int[] appWidgetIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
                if (appWidgetIds != null && appWidgetIds.length > 0) {
                    this.onUpdate(context, AppWidgetManager.getInstance(context), appWidgetIds);
                }
            }
        }
        else if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
        	Log.d("MyTestWidget", "ALARM_CANCLE CALL!!!!!!");
//        	Bundle bun = intent.getExtras();
//        	int uniqueId = bun.getInt("TESTKEY");
//        	String actionTaget = bun.getString("TESTACTION");
//        	Log.d("MyTestWidget", "CANCLE uniqueId : " + String.valueOf(uniqueId));
//        	Intent testIntent = new Intent(context, MySimpleWidget.class);
//        	testIntent.setAction(actionTaget);
//        	AlarmManagerController amController = new AlarmManagerController(context);
//        	amController.cancelAlarm(testIntent, uniqueId);
        	
//            Bundle extras = intent.getExtras();
//            if (extras != null && extras.containsKey(AppWidgetManager.EXTRA_APPWIDGET_ID)) {
//                final int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
//                this.onDeleted(context, new int[] { appWidgetId });
//            }
        }
        else if (AppWidgetManager.ACTION_APPWIDGET_ENABLED.equals(action)) {
            this.onEnabled(context);
        }
        else if (AppWidgetManager.ACTION_APPWIDGET_DISABLED.equals(action)) {
            this.onDisabled(context);
        }
	}
	
	
	@Override
	public void onEnabled(Context context) {
		// TODO Auto-generated method stub
		super.onEnabled(context);
	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		final int N = appWidgetIds.length;

        for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.simple_widget_layout_test);
            appWidgetManager.updateAppWidget(appWidgetId, views);
            
            Log.d("PasTest", Integer.toString(appWidgetId));
        }
	}
	

}
